using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 系统配置相关接口。
    /// 提供系统配置项的获取、设置、分组、刷新等 API。
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ConfigController : ControllerBase
    {
        /// <summary>
        /// 系统配置服务接口
        /// </summary>
        private readonly IConfigService _service;

        /// <summary>
        /// 构造函数，注入配置服务
        /// </summary>
        /// <param name="service">系统配置服务接口</param>
        public ConfigController(IConfigService service)
        {
            _service = service;
        }
        /// <summary>
        /// 获取指定配置项的值
        /// </summary>
        /// <param name="key">配置项键</param>
        /// <returns>配置项值</returns>
        [HttpGet("value/{key}")]
        public async Task<string?> GetValue(string key) => await _service.GetValueAsync(key);
        /// <summary>
        /// 获取指定分组的所有配置项
        /// </summary>
        /// <param name="category">配置分组</param>
        /// <returns>分组配置项字典</returns>
        [HttpGet("group/{category}")]
        public async Task<IDictionary<string, string>> GetGroup(string category) => await _service.GetGroupAsync(category);
        /// <summary>
        /// 设置配置项
        /// </summary>
        /// <param name="dto">系统设置数据传输对象</param>
        /// <returns>操作结果</returns>
        [HttpPost("set")]
        public async Task<IActionResult> SetValue([FromBody] SystemSettingDto dto)
        {
            await _service.SetValueAsync(dto.Key, dto.Value, dto.Category, dto.Description);
            return Ok();
        }
        /// <summary>
        /// 刷新配置缓存
        /// </summary>
        /// <returns>操作结果</returns>
        [HttpPost("refresh")]
        public async Task<IActionResult> Refresh()
        {
            await _service.RefreshAsync();
            return Ok();
        }
        /// <summary>
        /// 获取所有配置项
        /// </summary>
        /// <returns>系统设置项列表</returns>
        [HttpGet("all")]
        public async Task<IEnumerable<SystemSettingDto>> GetAll() => await _service.GetAllAsync();
    }
}
